import { NodeSize } from '../config'

export default function fieldSplitter(flow) {
  flow.register('FieldSplitter', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M76.068571 139.776v180.175238h871.862858v-180.175238H76.068571z m830.415239 138.752H117.51619V181.223619h788.96762v97.304381zM76.068571 884.248381H463.238095v-41.447619H117.51619v-97.328762H463.238095v-41.423238H76.068571zM560.761905 745.472h345.721905v97.328762H560.761905v41.447619h387.169524v-180.199619H560.761905z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M559.786667 771.900952h29.257143v44.470858h-29.257143zM433.493333 771.900952h29.257143v44.470858h-29.257143z'
            }
          ),
          h(
            'path',
            {
              fill: '#0CB20F',
              d: 'M722.285714 520.655238v73.849905l-53.686857-53.662476-20.699428 20.675047 74.361904 74.386286v2.486857h2.486857l20.699429 20.699429 20.699429-20.699429h2.486857v-2.486857l74.361905-74.386286-20.699429-20.675047-53.686857 53.662476v-73.849905a130.730667 130.730667 0 0 0-130.584381-130.584381h-101.595429v-65.194667h-46.323809v65.194667h-104.131048a130.730667 130.730667 0 0 0-130.584381 130.584381v61.756952l-47.640381-47.64038-32.768 32.768 103.570286 103.570285 103.570286-103.570285-32.768-32.768-47.640381 47.64038v-61.756952a84.358095 84.358095 0 0 1 84.260571-84.260571h252.050286a84.358095 84.358095 0 0 1 84.260571 84.260571z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '拆分字段'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
